import base from './../util/base'
import { observable, action, asMap, toJS } from 'mobx';
import * as mobx from 'mobx';
import * as API from './../apis/form'
import { Form } from 'weaver-mobile-page';
import * as _ from 'lodash';

export class FreeWorkflowSimple {

    @observable form = new Form.Store({ route: '/req' });

    conditioninfo = [];

    defaultFormDatas = {};

    reTraceFormData = null;

    toNewFormData = null;

    @action("控制变量")
    setState = (state = {}) => {
        Object.keys(state).forEach(key => {
            this[key] = state[key];
        });
    }

    loadFormCondition = (params) => {
        API.getTransferCondition(params).then((data) => {
            this.conditionData  = data;
            const {conditioninfo,default_prefix = '',default_datas = {}} = data;
            let condition  =  this.setDefaultData(conditioninfo,default_prefix,default_datas);
            this.conditioninfo = condition;
            this.form.initForm(condition, '/req');
            this.setFormSubmitDatas();
            let formParams = this.form.formParams;
            if (formParams.hasOwnProperty("isToNewAddSign") && formParams.isToNewAddSign == 1) {
                this.toNewFormData = this.form.formData;
            }
        });
    }

    /**
     * 设置默认值
     * @param conditioninfo
     * @param default_prefix
     * @param default_datas
     * @returns {Array}
     */
    setDefaultData = (conditioninfo = [],default_prefix = '',default_datas,v) => {
        // if(default_prefix == '') return conditioninfo;
        let { items = [] } = conditioninfo[0];
        let default_nodeOperator  = default_datas[`${default_prefix}_nodeOperator`] || [];
        let default_endTransfer  = default_datas[`${default_prefix}_endTransfer`] || 0;
        let default_countersignAttribute  = default_datas[`${default_prefix}_countersignAttribute`] || 0;
        let default_approvalSettings  = default_datas[`${default_prefix}_approvalSettings`] || '';
        let viewAttr = 2;
        const {isaffirmance = ''} = base.wfGlobalStore.urlParams;
        const {isFree='0'} = base.wfOperateStore.rightMenu;
        if(isaffirmance == 1) { 
            viewAttr = 1;
        } else {
            if(default_prefix == 'curr' && _.has(default_datas,'last_isvalild')) viewAttr = 3;
            if(default_prefix == 'last') viewAttr = 1;
        }
        items.map(item => {
            let useLastDatas = (!this.toNewFormData && (items.length == 4|| (isFree == 1 && isaffirmance !=1) )) && v==1;
            if(isaffirmance == 1 || item.domkey[0] !== 'isToNewAddSign') {
                item.viewAttr = viewAttr;
                if(item.browserConditionParam){
                    item.browserConditionParam.viewAttr = viewAttr;
                }
            }
            if (item.domkey[0] == 'nodeOperator') {
                item.browserConditionParam.replaceDatas = useLastDatas ?(default_datas[`last_nodeOperator`] || []) : default_nodeOperator;
            } else if (item.domkey[0] == 'endTransfer') {
                item.options.map(option => {
                    option.selected  = useLastDatas ? option.key == default_datas[`last_endTransfer`] : option.key == default_endTransfer;
                })
            } else if (item.domkey[0] == 'countersignAttribute') {
                item.options.map(option => {
                    option.selected  = useLastDatas ? option.key == default_datas[`last_countersignAttribute`] : option.key == default_countersignAttribute;
                })
            } else if (item.domkey[0] == 'approvalSettings') {
                item.value = default_approvalSettings;
                item.conditionType = 'CUSTOM';
                let otherParams = {multiChoose:true};
                item.otherParams = otherParams;
            }
        });
        return conditioninfo;
    }

    changeForm = (activeCondition,v) => {
        if(Number(v) < 2) {
            let {conditioninfo,default_datas = {}} = mobx.toJS(this.conditionData);
            let condition  = this.setDefaultData(conditioninfo,v == 0 ? 'last' : 'curr',default_datas,v);
            this.conditioninfo = condition;
            this.form = new Form.Store({ route: '/req' });
            this.form.setState({conditioninfo:condition})
            this.form.initForm(condition, '/req');
            if(v == 1) {
                this.toNewFormData && this.form.updateFields(this.toNewFormData);
            }
        }
        this.form.onChange(activeCondition, v)
        this.setFormSubmitDatas();
    } 

    setFormSubmitDatas = () => {
        let formParams = this.form.formParams || {}
        let submitParams = {
            operatorids: formParams.nodeOperator || "",
            countersignAttribute: formParams.countersignAttribute || "",
            isFlowToNext: formParams.endTransfer || "",
            isToNewAddSign: formParams.isToNewAddSign || "3",
            approvalSettings:formParams.approvalSettings ||''
        }
        this.chengeSubmitParams(submitParams);
    }

    chengeSubmitParams = (submitParams = {}) => {
        const { changeSubmitParam } = base.wfOperateStore;
        changeSubmitParam(submitParams)
    }
}